<?php
header("Content-Type: text/html; charset=iso-8859-1");
class equipe{
	var $fields;
	
	function equipe($id,$field = 'id'){
		$fields = describe("equipes");
		$q = "select * from equipes where $field = '$id'";
		$r = ExecRequete($q);
		if(!mysql_num_rows($r)){
			$this->id = 0;
		}
		else{
			$s = LigneSuivante($r);
			foreach($fields as $field => $type){
				$this->$field = $s->$field;
			}
			$q = "select classement from classements where id_equipe = $this->id order by date desc limit 1";
			$r = ExecRequete($q);
			$this->classement = ($s = LigneSuivante($r)) ? $s->classement : "?/?";
			$this->entraineurs = array();
			$q  = "select membre_id, concat(prenom,' ',nom) as nom from entraineurs, membres  where membre_id = membres.id and equipe_id = $this->id";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				// $entraineur = new membre($s->membre_id);
				$this->entraineurs[$s->membre_id] = $s->nom;				
			}
			$q = "select * from entrainements where equipe_id = '$this->id'";
			$r = ExecRequete($q);
			$this->entrainements = array();
			while($s = LigneSuivante($r)){
				$this->entrainements[] = $s->id;
			}
		}
	}
	
	function delete(){
		$q = "delete from equipes where id = '$this->id' limit 1";
		if(ExecRequete($q)){
			$q = "delete from membre_equipes where equipe_id = '$this->id'";
			if(ExecRequete($q)) return true;
			else return false;
		}	
		else return false;
		
	}
	
	function matchs($saison = ""){
		$matchs = array();
		if($saison){
			$table = (table_exists("matchs_$saison")) ? "matchs_$saison" : "matchs";
		}
		else $table = "matchs";
		$matchs = array();
		if($this->id){
			$q = "select no from $table where equipe = '$this->nom' order by date";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$matchs[] = new match($s->no,'no',$saison);
			}			
		}
		return $matchs;
	}

	function photo_iphone($taille = ""){
		if($taille) $taille = "-".$taille;
		$src = (file_exists("photos/equipes/".$this->id_vbcll."$taille.jpg")) ? $this->id_vbcll."$taille.jpg" : "";
		if($src){
			$size = getimagesize("photos/equipes/".$this->id_vbcll."$taille.jpg");
			return "<img class = 'shadow' id = 'photo' src = 'photos/equipes/$src' $size[3] />";			
		}
		else error_log("photos/equipes/".$this->id_vbcll."$taille.jpg");		
	}

	
	function photo($taille = ''){
		if($taille) $taille = "-".$taille;
		$src = (file_exists("photos/equipes/".$this->id_vbcll."$taille.jpg")) ? $this->id_vbcll."$taille.jpg" : "";
		if($src){
			$size = getimagesize("photos/equipes/".$this->id_vbcll."$taille.jpg");
			return "<div class='shadow'><img id = 'photo' src = 'photos/equipes/$src' $size[3] /><div class='topleft'></div><div class='topright'></div><div class='bottomleft'></div><div class='bottomright'></div></div>";			
		}
		// else error_log("photos/equipes/".$this->id_vbcll."$taille.jpg");
	}
	
	function resume($titre = 1){
		$jours = array("lu" => "lundi",
						"ma" => "mardi",
						"me" => "mercredi",
						"je" => "jeudi",
						"ve" => "vendredi",
						"sa" => "samedi",
						"di" => "dimanche");

		$onmouseover = ($titre) ? 'onmouseover = "bouton_equipe('.$this->id.')"' : "";
		echo "<div class = 'list' $onmouseover>\n";
		if($titre) echo "<div class = 'titre_equipe' onclick = 'equipe_detail($this->id)'><span class = 'bold'>$this->nom</span> : $this->nom_complet</div>\n";
		echo "<div class = 'content_equipe'>\n";
		$dims = getimagesize("photos/equipes/".$this->id_vbcll."-grand.jpg");
		
		$onclick = ($titre) ? "equipe_detail($this->id)" : "popitup(\"photos/equipes/".$this->id_vbcll."-grand.jpg\",$dims[0],$dims[1])";
		$left = "<span onclick = '$onclick' style = 'cursor: pointer'>".($this->photo())."</span>";
		echo leftCol($left,44);
		$right = "";
		#### entraîneur ######
		foreach($this->entraineurs as $membre_id => $membre_nom){
			$right .= leftCol("<span class = 'bold'>entra&icirc;neur</span>",34);
			$right .= rightCol("<a href = 'javascript:show_membre($membre_id)'>".ucwords(strtolower($membre_nom))."</a>",64);			
		}
		#### responsable ######
		$responsable = new membre($this->responsable);
		$right .= leftCol("<span class = 'bold'>responsable</span>",34);
		$right .= rightCol("<a href = 'javascript:show_membre($responsable->id)'>".ucwords(strtolower($responsable->prenom." ".$responsable->nom))."</a>",64);
		#### entrainements ####
		$right .= leftCol("<span class = 'bold'>entra&icirc;nements</span>",34);
		$entr = "<ol style = 'text-indent: 0; margin-top: 0; padding-top: 0; margin-left: 0; padding-left: 1.5em'>";
		$q = "select * from entrainements where equipe_id = '$this->id' and `match` = 0";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$salle = new salle($s->salle);
			$entr .= "<li>".$jours[$s->jour]." ".$s->heure."<br /> ".(ucwords(strtolower($salle->lien("0"))))."</li>";
		}
		$entr.= "</ol>\n";
		$right .= rightCol($entr,64);
		#### matchss ####
		$right .= leftCol("<span class = 'bold'>matchs domicile</span>",34);
		$entr = "<ol style = 'text-indent: 0; margin-top: 0; padding-top: 0; margin-left: 0; padding-left: 1.5em'>";
		$q = "select * from entrainements where equipe_id = '$this->id' and `match` = 1";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$salle = new salle($s->salle);
			$entr .= "<li>".$jours[$s->jour]." ".$s->heure."<br /> ".(ucwords(strtolower($salle->lien("0"))))."</li>";
		}
		$entr.= "</ol>\n";
		$right .= rightCol($entr,64);

		if($titre) $right .= "<br class = 'clear' /><p class = 'center'><button id = 'bouton_$this->id' type = 'button' style = 'display: none;' onclick = 'equipe_detail($this->id)'>page de l'&eacute;quipe</button></p>\n";
		echo rightCol("$right" ,54);
		echo "<br class = 'clear' /></div>\n";
		echo "</div>\n";
	}
	
	function add_entrainement($jour,$heure,$salle){
		if($jour && $heure && $salle){
			$q = "select * from entrainement where equipe_id = $this->id and jour = '$jour' and salle = '$salle' and heure = '$heure'";
			$r = ExecRequete($q);
			if(!mysql_num_rows($r)){
				$q = "insert into entrainements (equipe_id, salle, jour, heure) values ('$this->id', '$salle', '$jour','$heure')";
				return (ExecRequete($q)) ? true : false;
			}
			else return false;
		}
		else return false;
	}
	
	function delete_entrainement($id){
		$q = "delete from entrainements where id = $id and equipe_id = '$this->id'";
		return (ExecRequete($q)) ? true : false;
	}
	
	
	function est_membre($user_id){
		if(!$user_id) return 0;
		$q = "select membre_id from membre_equipes where equipe_id = '$this->id' and membre_id = '$user_id'";
		// error_log($q);
		$r = ExecRequete($q);
		if(mysql_num_rows($r)){
			return 1;
		}
		elseif(array_key_exists($user_id,$this->entraineurs)) return 1;
		elseif($this->responsable == $user_id) return 1;
		else return 0;
	}
	function list_membres($order = 'prenom, nom'){
		$membres = array();
		$q = "select membre_id from membre_equipes inner join membres on membre_id = membres.id where equipe_id = '$this->id' and membres.valide = 1 order by $order";
		$r  = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$membre = new membre($s->membre_id);
			$membres[$s->membre_id] = $membre;
		}
		foreach($this->entraineurs as $entr_id => $entr_nom){
			$membres[$entr_id] = new membre($entr_id);
		}
		$membres[$this->responsable] = new membre($this->responsable);
		return $membres;
	}

	function vcard(){
		$membres = $this->list_membres();
		$output = "";
		foreach($membres as $membre_id => $membre) $output .= $membre->vcard();
		return $output;
	}
	
	function list_joueurs($photo = 0){
		$titre = (strstr($this->nom,"F") !==  FALSE) ? "Les joueuses" : "Les joueurs";
		$vcard = (check_cookie()) ? "<a href = 'vcard.php?equipe=$this->id'><img src = 'images/icon_vcard.png' alt = 'vcard' title = 'vcard' height = '23' width = '29' /></a>" : "";
		$status = ($photo) ? "masquer les photos" : "afficher les photos";
		$param_photo = ($photo) ? 0 : 1;
		echo "<div id = 'membres_equipe'>\n";
		echo "<h2>$titre&nbsp;&nbsp;$vcard<span class = 'float_right' id = 'status_photo' ><a href = 'javascript:show_photos_equipe($this->id,$param_photo)'>$status</a></span></h2>\n";
		echo "<div style = 'margin: 0 20px'>\n";
		$q = "select membre_id from membres left join membre_equipes on membres.id = membre_id where equipe_id = '$this->id' and membres.valide = 1 order by nom, prenom";
		$r  = ExecRequete($q);		
		$nb = mysql_num_rows($r);
		$i = 0;
		$print = "";
		$est_membre = ($this->est_membre(check_cookie()) || check_admin());
		if(!$photo) $print .= "<ul class = 'nav'>";
		while($s = LigneSuivante($r)){
			$membre = new membre($s->membre_id);
			$compta = "";
			if($est_membre){
				$compta = "<span class = 'small fade'>(";
				$compta .= ($membre->comptabilite['licence']) ? "lic." : "";
				$compta .= ($membre->comptabilite['cotisation']) ? " coti." : "";
				$compta .= ")</span>";									
			}
			if($photo){
				if($i++ > ($nb/2)){ echo leftCol($print); $print = ""; $i = 0;}
				$print .= "<div class = 'list'>\n";
				$print .= "<div class = 'content_equipe'>\n";
				$print .= leftCol($membre->photo('40'),20);
				$print .= rightCol("<a href = 'javascript:show_membre($membre->id)'>".$membre->prenom." ".$membre->nom." </a> $compta",75);
				$print .= "</div></div>\n";				
			}
			else $print .= "<li style = 'float: left; width: 33%' ><a href = 'javascript:show_membre($membre->id)' class = 'hover_underline'>".$membre->prenom." ".$membre->nom."</a> $compta</li>\n";
		}
		echo ($photo) ? rightCol($print) : $print."</ul>";
		echo "<br class = 'clear' />";
		echo "</div>\n";
		echo "</div>\n";
	}
	
	function calendrier($saison = "",$edit=0){
		if($saison) $saison = (table_exists("matchs_$saison")) ? $saison : "";
		$matchs = $this->matchs($saison);
		$table = new table('calendrier_'.$this->id);
		$table->nb_rows = count($matchs);
		$table->filter = 0;
		$table->order = 'date';
		$fields = array("no","jour","date","heure","salle","local","visiteur","resultat");
		if(!$edit) $fields[] = 'stat';
		foreach($fields as $field){
			$$field = new table_field($field,'calendrier_'.$this->id);
			if($field == 'no') $no->hidden = 1;
			elseif($field == 'stat') $stat->styleclass = 'nowrap_center';
			elseif($field == 'resultat') $resultat->styleclass = 'nowrap_center';
			$table->fields[] = $$field;
		}
		if($edit){
			$action = new table_field('action','calendrier_'.$this->id);
			$action->styleclass = 'nowrap';
			$table->fields[] = $action;	
		} 
		$id->hidden = 1;
		$i = 0;
		$ok_edit = $this->est_membre(check_cookie());
		foreach($matchs as $match){
			$row = new table_row();
			$row->onclick = "";
			foreach($fields as $field){
				if($field == 'resultat'){
					if(!$match->resultat  && $ok_edit == 1 && $match->date <= date("Y-m-d")){
						$echo = "<select name = '$match->no' onchange = \"if(this.options[this.selectedIndex].value) update_resultat('$match->no',this.options[this.selectedIndex].value,'equipe')\">";
						$echo .= "<option value = ''>score...</option>";
						$tab_scores = ((strpos($this->nom,'M16')!==FALSE)||(strpos($this->nom,'M19G')!==FALSE)) ? array("2-0","2-1","1-2","0-2") : array("3-0","3-1","3-2","2-3","1-3","0-3");
						foreach($tab_scores as $score) $echo .= "<option value = '$score'>$score</option>";
						$echo .= "</select><span id = 'ok_$match->no'></span>";
					}
					else $echo = $match->format_resultat();
					if($match->resultat){
						$tab_resultat = explode("-",$match->resultat);
						if(strstr($match->format_resultat(),"gagne") !== FALSE){
							$_SESSION['scores'][] = abs($tab_resultat[0]-$tab_resultat[1]);
						}
						else $_SESSION['scores'][] = -abs($tab_resultat[0]-$tab_resultat[1]);
						$_SESSION['label_scores'][] = $match->adversaire;						
					}
				} 
				elseif($field == 'salle'){
					$salle = new salle($match->$field);
					$echo = $salle->lien();
				}
				elseif($field == 'stat'){
					$echo = "<a href = 'javascript:stats_match(\"$match->no\")'>".$match->$field."</a>\n";
				}
				else $echo = str_ireplace("vbc ","",$match->$field);
				$row->cols[$field] = $echo;
			}
			$key = key($table->rows) + 1;
			if($edit) $row->cols['action'] = "<img src = 'images/icon_edit.png' alt = 'edit' height = '14' width = '14' onclick = 'form_edit_match(\"$match->no\",$i)' /><img src = 'images/icon_delete.png' alt = 'supp.' height = '14' width = '14' onclick = 'if(confirm(\"Veuillez confirmer la suppression du match $match->no\")) del_match(\"$match->no\",$key)' />";
			$table->rows[] = $row; 
			$i++;
		}
		if($edit){
			$footer = "\n<tr>";
			for($i = 0; $i < count($fields)-1; $i++) $footer .= "<td></td>";
			$footer .= "<td class = 'center'><img src = 'images/icon_new.png' alt = 'nouveau' class = 'pointer' height = '14' width = '14' onclick = 'form_new_match(\"$this->nom\")' /></td></tr>";
			// $table->rows[] = $row; 
			// $i++;			
		}
		else $footer = "";
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer($footer);			
	}
	
	function list_matchs($saison = ""){

		if($saison) $saison = (table_exists("matchs_$saison")) ? $saison : "";
		if(isset($_SESSION['scores'])) unset($_SESSION['scores']);
		if(isset($_SESSION['label_scores'])) unset($_SESSION['label_scores']);	
		echo "<div id = 'stats_equipe'>\n";	
		echo "<h2>Les matchs<span class = 'float_right'><pre id = 'nom_cal' style = \"visibility:hidden; display: none;padding-right: 1em;vertical-align: top;font-size: 9pt;\">fichier iCal</pre><a href = 'ical.php?equipe=$this->nom&table=calendrier_".$this->id."'><img src = 'images/icon_ical.png' alt = 'ical' title = 'ical' height = '23' width = '23' onmouseover = \"show_el('nom_cal')\"; onmouseout = \"hide_el('nom_cal')\"/></a></span></h2>\n";
		echo "<div class = 'center'>\n";
		$style = ($saison) ? "style = 'display: none;visibility:hidden;'" : "";
		echo "<p class  ='center' id = 'saison_actuelle' $style><span class = 'bold'>Classement actuel : <a href = 'javascript:print_classement(\"$this->id\")'>$this->classement</a></span> (<a href = 'javascript:show_change_saison(1)'>saisons pr&eacute;c&eacute;dentes</a>)</p>\n";
		$style = (!$saison) ? "style = 'display: none;visibility:hidden;'" : "";
		echo "<p class = 'center'  id = 'choix_saison' $style><span class = 'bold'>Saison&nbsp;</span><select name = 'saison' onchange = 'if(this.options[this.selectedIndex].value) change_saison($this->id,this.options[this.selectedIndex].value)'>\n";
		echo "<option value = ''>--saison---</option>\n";
		$q = "show tables";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			if(ereg("matchs_([0-9]{2}_[0-9]{2})",$s->{"Tables_in_".BASE},$regs)){
				$selected = ($saison == $regs[1]) ? "selected = 'selected'" : "";
				echo "<option value = '$regs[1]' $selected>$regs[1]</option>\n";	
			} 
		}
		echo "</select>(<a href = 'javascript:show_change_saison(0);change_saison($this->id,\"\")'>saison acutelle</a>)</p>\n";
		echo "</div>\n";
		
		$this->calendrier($saison);
		
######## Présences ##############
		if($this->est_membre(check_cookie())){
			echo "<h2><a name = 'presences'></a>Liste des pr&eacute;sences <span class = 'float_right'>";
			echo (isset($_SESSION['prochain_presences']) && $_SESSION['prochain_presences'] ) ? "<a href = '#presences' onclick = 'prochain_presences($this->id);return false;' id = 'link_prochain_presences'>tous les matchs</a>" : "<a href = '#presences' onclick = 'prochain_presences($this->id);return false;' id = 'link_prochain_presences'>prochains matchs</a>";
			echo "</span></h2>\n";
			echo "<div id = 'presences' style = 'width: 600px;'>\n";
			echo $this->print_presences(0);
			echo "</div>\n";
		}

##### Stats ########
		if(isset($_SESSION['datay'])) unset($_SESSION['datay']);
		if(isset($_SESSION['labelx'])) unset($_SESSION['labelx']);		
		if($saison){
			if(table_exists("matchs_$saison")) $scores = 1;
			if(table_exists("classements_$saison")){
				$classement = 1;
			}
		}
		else{
			$classement = 1;
			$scores = 1;
		}
		echo "<h2>Evolution du classement</h2>\n";
		if($saison) $saison = "_".$saison;
		if(!$classement){
			echo "<p class = 'center fade'>aucune donn&eacute;e disponible</p>\n";
		}
		else{
			$q = "select date, classement from classements$saison where id_equipe = $this->id order by date";
			$r = ExecRequete($q);
			$_SESSION['labelx'] = array();
			$_SESSION['datay'] = array();
			while($s = LigneSuivante($r)){
				$tab = explode("/",$s->classement);
				if(count($tab)>1){
					$_SESSION['datay'][] = $tab[0];
					$_SESSION['max_y'] = $tab[1];
					$_SESSION['labelx'][] = substr(preg_replace("/<\/*span.*?>/","",format_date($s->date)),0,5);
				}
			}
			if(count($_SESSION['datay']) > 1) echo ('<img src="create_graph.php?x=labelx&y=datay&type=classement&'.microtime().'" border=0 align=center width =600 height= 200>');			
			elseif(count($_SESSION['datay']) == 1) echo "<p class = 'center fade'>donn&eacute;e insuffisante</p>\n";
			else echo "<p class = 'center fade'>aucune donn&eacute;e disponible</p>\n";
		}
		if(!$scores){
			echo "<p class = 'center fade'>aucune donn&eacute;e disponible</p>\n";
		}
		else{
			echo "<h2>Profil des r&eacute;sultats</h2>\n";
			$q = "select * from matchs where equipe = '$this->nom' and resultat != '' and resultat != 'NULL'";
//			error_log($q);
			$r = ExecRequete($q);
			$nb = mysql_num_rows($r);
			if($nb > 1) echo ('<img src="create_graph.php?x=label_scores&y=scores&type=equipe&'.microtime().'" border=0 align=center width =600 height= 300>');
			elseif($nb == 1) echo "<p class = 'center fade'>donn&eacute;e insuffisante</p>\n";
			else echo "<p class = 'center fade'>aucune donn&eacute;e disponible</p>\n";
			echo "</div>\n";			
		}
	}
	function form_communication(){
		$user_id = check_cookie();
		if($this->est_membre($user_id)){
			echo "<a name = 'communications'></a><div id = 'ajout_communication'>\n";
			echo "	<form action = 'javascript:ajout_communication(document.myform.membre.value, document.myform.contenu.value, document.myform.equipe.value, document.myform.email.checked)' method = 'post'  name = 'myform'>\n";
			$titre = "<input type = 'hidden' name = 'membre' value = '$user_id' />Ajouter une communication";
			$content = "<p class = 'center'><button type = 'button' name = 'citation' onclick = 'inserttag(\"_\",document.myform.contenu)'>_italique_</button>\n";
			$content .= "<button type = 'button' name = 'citation' onclick = 'inserttag(\"*\",document.myform.contenu)'>*gras*</button>\n";		
			$content .= "<button type = 'button' name = 'citation' onclick = 'insertcode(\"\\n\\ncit%Auteur%. Texte%%\\n\\n\",document.myform.contenu)'>citation</button>\n";
			$content .= "<button type = 'button' name = 'citation' onclick = 'insertcode(\" &#39;&#39;Titre&#39;&#39;:http://adresse \",document.myform.contenu)'>lien</button></p>\n";
			$content .= "<p class = 'center'><input type = 'hidden' name = 'equipe' value = '$this->id' />\n";
			$content .= "<textarea cols = '60' rows = '10' name = 'contenu'></textarea></p>\n";
			$content .= "<p class = 'center'><input type = 'checkbox' name = 'email' value = '1' /> envoyer aux membres de l'&eacute;quipe<br /><button type = 'submit'>ajouter</button></p>\n";
			echo box($titre,$content,600);
			echo "	</form>\n";
			echo "</div>\n";
		}		
	}

	function list_communications(){
		echo "<div id = 'communications'>\n";
		echo "<h2>Communications internes</h2>\n";
		$q = "select id from communications where equipe_id = $this->id and date >= date_sub(curdate(),interval 60 day) order by date desc";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$communication = new communication($s->id);
			$communication->print_this();
		}
		if(!mysql_num_rows($r)) echo "<p class = 'center fade'>aucune communication</p>\n";
		$this->form_communication();
		echo "</div>\n";
	}
	
	function form_edit(){
		
	}
	
	function add_joueur($membre_id){
		$q = "select id from membre_equipes where equipe_id = '$this->id' and membre_id = '$membre_id'";
		$r = ExecRequete($q);
		if(!mysql_num_rows($r)){
			$q = "insert into membre_equipes (membre_id, equipe_id) values ($membre_id, $this->id)";
			if(ExecRequete($q)) return true;
			else return false;
		}
		else return false;
	}
	function print_presences(){
		if(!isset($_SESSION['presences'])) $this->liste_presences();
		$tab = $_SESSION['presences'];
		$equipe = (!empty($_SESSION['presences_equipe'])) ? $_SESSION['presences_equipe'] : "";
		echo "<div style = 'float: left;width: 150px;'>\n";
		echo "<table style = 'width: 100%'><tr style = 'line-height: 16px; height: 16px'>\n";
		for($j = 0; $j < count($tab); $j++){
			$td = ($j) ? "<td " : "<th ";
			$td_ = ($j) ? "</td>\n" : "</th>\n";
			for($i = 0; $i < 2; $i++){
				echo $td.$tab[$j][$i].$td_;
			}
			echo "</tr>\n";
			if($j < count($tab)-1) echo "<tr style = 'line-height: 16px; height: 16px'>\n";
		}
		echo "</table>\n";
		echo "</div>\n";
		if(count($tab[1])>2){
			echo "<div style = 'overflow: auto;width: 450px;' id = 'scroll_table'>\n";
			echo "<table  style = 'width: 100%'><tr style = 'line-height: 16px; height: 16px'>\n";
			for($j = 0; $j < count($tab); $j++){
				$td = ($j) ? "<td " : "<th ";
				$td_ = ($j) ? "</td>\n" : "</th>\n";
				for($i = 2; $i < count($tab[$j]); $i++){
					echo $td.$tab[$j][$i].$td_;
				}
				echo "</tr>\n";
				if($j < count($tab)-1) echo "<tr style = 'line-height: 16px; height: 16px'>\n";
			}
			echo "</table>\n";
			echo "</div>\n";			
		}
		else echo "<div style = 'width: 450px; height: 100px; padding-top: 45px' class = 'fade center'>le championnat est termin&eacute;</div>";
		echo "<div style = 'clear: left'></div>";
	}
	function liste_presences(){
		$membres = array();
		$tab = array();
		$_SESSION['presences_equipe'] = $this->id;
		$matchs = array();
		$total = array();
		$prochain = (isset($_SESSION['prochain_presences'])) ?  " and date >= curdate() " : "";
		$q = "select no, date_format(date,'%d.%m') as datef from matchs where equipe = '$this->nom' $prochain order by date";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)) $matchs[$s->no] = $s->datef;
		$tab[0] = array();
		$tab[0][] = '>joueur';
		$tab[0][] = ">status";
		foreach($matchs as $no => $date){
			$tab[0][] = ">".$date;
			$total[$no] = 0;
		} 

		$row = 0;
		$q = "select membre_matchs.no_match,membres.id as membre_id, licence, concat(membres.prenom,' ',substr(membres.nom,1,1),'.') as nom from membre_matchs right join membres on membres.id = membre_matchs.membre_id inner join membre_equipes on membre_equipes.membre_id = membres.id left join comptabilite on comptabilite.membre_id = membres.id where equipe_id = $this->id and membres.valide = 1 order by membres.prenom, membres.nom";
		$r = ExecRequete($q);
		$presences = array();
		$licences = array();
		while($s = LigneSuivante($r)){
			if(!isset($membres[$s->membre_id])) $membres[$s->membre_id] = $s->nom;
			if(!isset($presences[$s->membre_id])) $presences[$s->membre_id] = array();
			if($s->no_match) $presences[$s->membre_id][$s->no_match] = 1;
			if(!isset($licences[$s->membre_id])) $licences[$s->membre_id] = $s->licence;
		}
		foreach($membres as $id => $membre_nom){
			if($membre_nom){
				$row++;
				$tab[$row] = array();
				$modif = 0;
				if(check_cookie() == $id) $modif= 1;
				foreach($this->entraineurs as $entr_id => $entr_nom){
					if(check_cookie() == $entr_id) $modif = 2;
				}
				if($this->responsable == check_cookie()) $modif = 2;
				// elseif(check_cookie() == 88) $modif = 2;
				$licence = (isset($licences[$id])) ? $licences[$id] : 0;
				$class1 = ($licence) ? "" : "fade ";
				$status = ($licence) ? "" : "sans licence";
				$tab[$row][] = "class = 'center $class1 nowrap'>$membre_nom"; 
				$tab[$row][] = "class = 'center $class1 nowrap'>$status\n";
				foreach($matchs as $no => $date){
					$class = (array_key_exists($no,$presences[$id]) && $class1 != 'fade ') ? "vert" : "rouge";
					$class = $class1.$class;
					if($class == 'vert') $total[$no]++;
					if($modif){
						$checked = (array_key_exists($no,$presences[$id])) ? "checked = 'checked'" : "";
						$input = "<input type = 'checkbox' $checked onclick = 'update_presence($id,\"$no\",\"$this->id\")' style = 'height: 14px; line-height: 14px; padding: 0; margin: 1px 0'/>";					
					}
					else $input = '&nbsp;';
					$tab[$row][] = "class = '$class center'>$input";
				}
			}
		}
		$row++;
		$tab[$row] = array();
		$tab[$row][] = "class = 'center'>TOTAL"; 
		$tab[$row][] = "class = 'center'>&nbsp;";
		foreach($matchs as $no => $date){
			$tab[$row][] = "class = 'center bold' id = 'total_$no'><input type = 'hidden' id = 'total_value_$no' name = 'total_value_$no' value = '".$total[$no]."' />".$total[$no];
		} 
		$_SESSION['presences'] = $tab;
	}
	
	function print_classement(){
		$q = "select date from classements where id_equipe = '$this->id' order by date desc limit 1";
		$r = ExecRequete($q);
		if($s = LigneSuivante($r)){
			echo "<h2> Classement $this->nom_complet du ".(format_date($s->date))."</h2>\n";
			if(is_readable("classements/$this->id_vbcll.html")){
				if($this->id_vbcll == 'M1') echo utf8_decode(file_get_contents("classements/$this->id_vbcll.html"));
				else echo file_get_contents("classements/$this->id_vbcll.html");
			}
			else echo "<p class = 'fade center'>Aucune donn&eacute;e disponible</p>\n";
		}
		else echo "<p class = 'fade center'>Aucune donn&eacute;e disponible</p>\n";
	}
	
	function get_emails(){
		$q = "select group_concat(email) as emails from membres where id in (select membres.id from membre_equipes inner join membres on membre_id = membres.id where equipe_id = $this->id union (select membres.id from membres inner join entraineurs on entraineurs.membre_id = membres.id where equipe_id = $this->id) union (select membres.id from membres inner join equipes on equipes.responsable = membres.id where equipes.id = $this->id)) and email != '' and email is not null and membres.valide = 1 ";
		$r = ExecRequete($q);
		return ($s = LigneSuivante($r)) ? str_replace(",",", ",$s->emails) : "";
	}
}
?>
